package com.yfp.goods.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yfp.common.core.constant.DbConstants;
import com.yfp.common.core.exception.ServiceException;
import com.yfp.common.core.utils.uuid.Snowflake;
import com.yfp.common.datascope.annotation.DataScope;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.goods.domain.TagCombo;
import com.yfp.goods.mapper.ComboIsTagMapper;
import com.yfp.goods.mapper.TagComboMapper;
import com.yfp.goods.service.TagComboService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 套餐标签(TagCombo)表服务实现类
 *
 * 
 * @since 2023-12-27 14:20:28
 */
@Service("tagComboService")
public class TagComboServiceImpl implements TagComboService {
    @Resource
    private TagComboMapper tagComboMapper;

    @Resource
    private ComboIsTagMapper comboIsTagMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param tagId 主键
     * @return 实例对象
     */
    @Override
    public TagCombo queryById(String tagId) {
        return this.tagComboMapper.queryById(tagId);
    }

    /**
     * 分页查询
     *
     * @param tagCombo 筛选条件
     * @return 查询结果
     */
    @Override
    @DataScope(deptAlias = "c")
    public List<TagCombo> queryAll(TagCombo tagCombo) {
        return this.tagComboMapper.queryAll(tagCombo);
    }

    /**
     * 新增数据
     *
     * @param tagCombo 实例对象
     * @return 实例对象
     */
    @Override
    public TagCombo insert(TagCombo tagCombo)
    {
        this.tagComboMapper.insert(tagCombo);
        return tagCombo;
    }

    /**
     * 修改数据
     *
     * @param tagCombo 实例对象
     * @return 实例对象
     */
    @Override
    public TagCombo update(TagCombo tagCombo)
    {
        this.tagComboMapper.update(tagCombo);
        return this.queryById(tagCombo.getTagId());
    }

    /**
     * 新增套餐标签
     *
     * @param tagCombo
     */
    @Override
    public TagCombo addComboTag(TagCombo tagCombo)
    {
        tagNameVerifyIfThereAreDuplicates(tagCombo.getTagName(),null,tagCombo.getCityName());

        Snowflake snowflake = new Snowflake(1);
        tagCombo.setTagId(String.valueOf(snowflake.nextId()));
        tagCombo.setCreateOper(SecurityUtils.getLoginUser().getSysUser().getNickName());
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        tagCombo.setDeptId(deptId);
        this.tagComboMapper.insert(tagCombo);
        return tagCombo;
    }

    /**
     * 套餐标签名验证是否存在重复
     *
     * @param tagName 套餐标签
     */
    private void tagNameVerifyIfThereAreDuplicates(String tagName,String tagId,String cityName) {
        long count = this.tagComboMapper.countByTagName(tagName,tagId,cityName);
        if (count > 0) throw new ServiceException("标签名称已存在");
    }

    /**
     * 更新套餐标签
     *
     * @return {@link TagCombo}
     */
    @Override
    public TagCombo updateComboTag(String tagId, String tagName, String color, String cityName) {
        if (CharSequenceUtil.isEmpty(tagId) || CharSequenceUtil.isEmpty(tagName)) {
            throw new ServiceException("缺少必填参数");
        }
        tagNameVerifyIfThereAreDuplicates(tagName, tagId, cityName);

        TagCombo tagCombo = new TagCombo();
        tagCombo.setTagId(tagId);
        tagCombo.setTagName(tagName);
        tagCombo.setColor(color);
        tagCombo.setCityName(cityName);
        tagCombo.setUpdateTime(new Date());
        this.tagComboMapper.update(tagCombo);

        return tagCombo;
    }

    /**
     * 禁用/启用套餐标签
     *
     * @param tagId 套餐标签id
     * @return boolean
     */
    @Override
    public boolean disableTag(String tagId)
    {

        TagCombo tagCombo = this.tagComboMapper.queryById(tagId);
        if (ObjectUtil.isNull(tagCombo)) throw new ServiceException("套餐标签不存在",201);

        long count = comboIsTagMapper.queryComboIsTagByTagId(tagId);
        if (count > 0) throw new ServiceException("套餐标签已被绑定");

        boolean result = Objects.equals(DbConstants.DISABLED_ABNORMAL, tagCombo.getEnable());
        tagCombo.setUpdateTime(new Date());

        tagCombo.setEnable(result ? DbConstants.ENABLE_NORMAL : DbConstants.DISABLED_ABNORMAL);

        return this.tagComboMapper.update(tagCombo) > 0;
    }

    @Override
    public List<String> queryByTagIds(List<String> tagIds) {
        return this.tagComboMapper.queryByTagIds(tagIds);
    }
}
